package com.zhou;

import com.zhou.dao.StudentDao;
import com.zhou.domain.PrimaryStudent;
import com.zhou.domain.Student;
import com.zhou.util.QueryParam;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertTrue;


public class test18 {
    SqlSession sqlSession = null;
    StudentDao studentDao = null;
    /**
     * Rigorous Test :-)
     */
    @Test
    public void shouldAnswerWithTrue()
    {
        assertTrue( true );
    }

    @Before
    public void before() throws IOException {
        String config = "mybatis-config.xml";
        InputStream in = Resources.getResourceAsStream(config);
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        sqlSession = factory.openSession();
        studentDao = sqlSession.getMapper(StudentDao.class);
    }
    @After
    public void after(){
        sqlSession.close();
    }
    
    @Test
    public void testRetunInt(){
        int count =  studentDao.countStudent();
        System.out.println("学生总人数:"+count);
    }
    @Test
    public void testReturnMap(){
        Map<Object,Object> retMap = studentDao.selectReturnMap(1);
        System.out.println("查询结果是Map:" +retMap);
    }

    @Test
    public void testSelectUseResultMap(){
        QueryParam param = new QueryParam();
        param.setQueryName("张三");
        param.setQueryAge(25);
        List<Student> stuList = studentDao.selectUseResultMap(param);
        stuList.forEach(stu ->System.out.println(stu));
    }

    @Test
    public void testSelectUseFieldAlias(){
        QueryParam param = new QueryParam();
        param.setQueryName("张三");
        param.setQueryAge(20);
        List<PrimaryStudent> stuList = studentDao.selectUseFieldAlias(param);
        stuList.forEach(stu -> System.out.println(stu));
    }

    @Test
    public void testSelectUseDiffResultMap(){
        QueryParam param = new QueryParam();
        param.setQueryName("张三");
        param.setQueryAge(20);
        List<PrimaryStudent> stuList = studentDao.selectDiffResultMap(param);
        stuList.forEach(stu -> System.out.println(stu));
    }

    @Test
    public void testSelectLikeOne(){
        String name = "%张%";
        List<Student> stuList = studentDao.selectLikeFirst(name);
        stuList.forEach(stu -> System.out.println(stu));
    }
    @Test
    public void testSelectLikeSecond(){
        String name = "%张%";
        List<Student> stuList = studentDao.selectLikeSecond(name);
        stuList.forEach(stu -> System.out.println(stu));
    }

    @Test
    public void testSelect(){
        Student param = new Student();
        param.setName("张三");
        param.setAge(18);

        List<Student> studentList = studentDao.selectSudentIf(param);
        studentList.forEach(stu -> System.out.println(stu));
    }

    @Test
    public void testSelectWhere(){
        Student param = new Student();
        param.setName("张三");
        param.setAge(18);

        List<Student> studentList = studentDao.selectStudentWhere(param);
        studentList.forEach(stu -> System.out.println(stu));
    }
    @Test
    public void testSelectForList(){
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);

        List<Student> studentList = studentDao.selectStudentForList(list);
        studentList.forEach(stu -> System.out.println(stu));
    }
    @Test
    public void testSelectForList2() {
        List<Student> list =new ArrayList<>();
        Student s1 = new Student();
        s1.setId(1);
        list.add(s1);

        s1 = new Student();
        s1.setId(2);
        list.add(s1);

        List<Student> studentList = studentDao.selectStudentForList2(list);
        studentList.forEach(stu -> System.out.println(stu));
    }

    @Test
    public void testselectStudentSqlFragment() {
        List<Student> list =new ArrayList<>();
        Student s1 = new Student();
        s1.setId(1);
        list.add(s1);

        s1 = new Student();
        s1.setId(2);
        list.add(s1);

        List<Student> studentList = studentDao.selectStudentSqlFragment(list);
        studentList.forEach(stu -> System.out.println(stu));
    }


}
